#pragma once

#include "event/znx_conn.h"
#include "event/znx_openssl.h"
#include "event/znx_event_loop.h"
#include "http/znx_http_log.h"


typedef enum {
    ZNX_HTTP_NET_CONN   = 0,
    ZNX_HTTP_SSL_CONN   = 1
} znx_http_conn_type_t;


typedef struct znx_http_conn_s      znx_http_conn_t;
typedef void (*znx_http_conn_event_handler)(znx_http_conn_t *hc);


struct znx_http_conn_s {
    znx_conn_t                      *c;
    znx_http_conn_type_t            type;
    znx_http_log_t                  hlog;

    // context
    void                            *data;
    znx_http_conn_event_handler     read_handler;
    znx_http_conn_event_handler     write_handler;
    znx_http_conn_event_handler     close_handler;

    znx_buf_t                       *pre_buf;

    unsigned                        read_active:1;
    unsigned                        write_active:1;
};


znx_bool_t znx_http_conn_start_io(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_stop_io(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_enable_read(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_disable_read(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_read_ready(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_enable_write(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_disable_write(znx_http_conn_t *hc);

znx_bool_t znx_http_conn_write_ready(znx_http_conn_t *hc);

znx_result_t znx_http_conn_close(znx_http_conn_t *hc);

void znx_http_net_conn_event_handler(znx_event_t *event);

znx_result_t znx_http_conn_read(znx_http_conn_t *hc, void *buf, size_t count);

znx_result_t znx_http_conn_readv(znx_http_conn_t *hc, znx_iovec_t *vector, int count);

znx_result_t znx_http_conn_write(znx_http_conn_t *hc, void *buf, size_t count);

znx_result_t znx_http_conn_writev(znx_http_conn_t *hc, znx_iovec_t *vector, int count);


static inline void
znx_http_conn_init(znx_http_conn_t *hc,
    znx_conn_t *c, znx_http_conn_type_t type)
{
    hc->c = c;
    c->data = hc;
    hc->type = type;
    hc->c->read.handler = znx_http_net_conn_event_handler;
    hc->c->write.handler = znx_http_net_conn_event_handler;
}
